package com.java110.mall.chain.supplier.adaper.common;

import com.java110.mall.chain.supplier.ISupplierAdapter;
import com.java110.dto.chain.ChainSupplierProductDto;
import com.java110.dto.chain.ChainProductDto;
import com.java110.dto.chain.ChainProductDetailDto;
import com.java110.dto.chain.ChainProductValueDto;
import com.java110.dto.chain.ChainSupplierDto;
import com.java110.dto.chain.ChainSupplierCatalogDto;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.intf.chain.IChainProductDetailV1InnerServiceSMO;
import com.java110.intf.chain.IChainProductV1InnerServiceSMO;
import com.java110.intf.chain.IChainProductValueV1InnerServiceSMO;
import com.java110.intf.chain.IChainSupplierCatalogV1InnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用供应商对接适配器
 * 一般提供供应商 对接自己的系统
 */
@Component
public class DefaultSupplierAdapter implements ISupplierAdapter {

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;

    @Autowired
    private IChainProductV1InnerServiceSMO chainProductV1InnerServiceSMOImpl;

    @Autowired
    private IChainProductValueV1InnerServiceSMO chainProductValueV1InnerServiceSMOImpl;

    @Autowired
    private IChainProductDetailV1InnerServiceSMO chainProductDetailV1InnerServiceSMOImpl;

    @Autowired
    private IChainSupplierCatalogV1InnerServiceSMO chainSupplierCatalogV1InnerServiceSMO;

    @Override
    public ResultVo validateChainOrder(StoreOrderDto storeOrderDto, List<StoreOrderCartDto> storeOrderCartDtos, ChainSupplierDto chainSupplierDto) throws Exception {
        return new ResultVo(ResultVo.CODE_OK, "成功");
    }

    @Override
    public ResultVo notifyChainOrder(StoreOrderDto storeOrderDto, List<StoreOrderCartDto> storeOrderCartDtos, ChainSupplierDto chainSupplierDto) throws Exception {
        return new ResultVo(ResultVo.CODE_OK, "成功");
    }

    /**
     * 查询商品
     *
     * @param chainSupplierProductDto 订单调用链数据
     * @param chainSupplierDto
     * @return
     * @throws Exception
     */
    @Override
    public ResultVo queryProducts(ChainSupplierProductDto chainSupplierProductDto, ChainSupplierDto chainSupplierDto) throws Exception {
        ChainProductDto chainProductDto = BeanConvertUtil.covertBean(chainSupplierProductDto, ChainProductDto.class);

        int count = chainProductV1InnerServiceSMOImpl.queryChainProductsCount(chainProductDto);

        List<ChainProductDto> chainProductDtos = null;

        if (count > 0) {
            chainProductDtos = chainProductV1InnerServiceSMOImpl.queryChainProducts(chainProductDto);
            freshProductDtos(chainProductDtos);
        } else {
            chainProductDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) chainSupplierProductDto.getRow()), count, chainProductDtos);

        return resultVo;

    }

    private void freshProductDtos(List<ChainProductDto> productDtos) {

        if (productDtos == null || productDtos.size() < 1) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();

        for (ChainProductDto tmpProductDto : productDtos) {
            productIds.add(tmpProductDto.getProductId());
        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (ChainProductDto tmpProductDto : productDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }
                if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                    url = tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }
        }

        for (ChainProductDto tmpProduct : productDtos) {
            ChainProductValueDto chainProductValueDto = new ChainProductValueDto();
            chainProductValueDto.setCsId(tmpProduct.getCsId());
            chainProductValueDto.setProductId(tmpProduct.getProductId());
            List<ChainProductValueDto> productValueDtos = chainProductValueV1InnerServiceSMOImpl.queryChainProductValues(chainProductValueDto);
            if (productValueDtos == null || productValueDtos.size() < 1) {
                return;
            }
            tmpProduct.setChainProductValueDtos(productValueDtos);

            ChainSupplierCatalogDto chainSupplierCatalogDto = new ChainSupplierCatalogDto();
            chainSupplierCatalogDto.setCatalogId(tmpProduct.getCatalogId());
            List<ChainSupplierCatalogDto> queryChainSupplierCatalogs = chainSupplierCatalogV1InnerServiceSMO.queryChainSupplierCatalogs(chainSupplierCatalogDto);
            if(queryChainSupplierCatalogs.size() > 0){
                tmpProduct.setCatalogName(queryChainSupplierCatalogs.get(0).getCatalogName());
            }
        }

        //输入轮播图
        fileRelDto = new FileRelDto();
        fileRelDto.setObjId(productDtos.get(0).getProductId());
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE);
        fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        List<String> photos = new ArrayList<>();
        for (ChainProductDto tmpProductDto : productDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    url = imgUrl + tmpFileRelDto.getFileRealName();
                    photos.add(url);
                }
            }
            tmpProductDto.setCarouselFigurePhotos(photos);

            ChainProductDetailDto chainProductDetailDto = new ChainProductDetailDto();
            chainProductDetailDto.setProductId(tmpProductDto.getProductId());
            chainProductDetailDto.setCsId(tmpProductDto.getCsId());
            List<ChainProductDetailDto> productDetailDtos = chainProductDetailV1InnerServiceSMOImpl.queryChainProductDetails(chainProductDetailDto);
            if (productDetailDtos == null || productDetailDtos.size() < 1) {
                return;
            }
            tmpProductDto.setContent(productDetailDtos.get(0).getContent());
        }
    }

    /**
     * <p>查询单个商品信息</p>
     *
     *
     * @param chainSupplierProductDto  订单调用链数据
     * @return RoomDto 对象数据
     */
    @Override
    public ResultVo querySingleProduct(ChainSupplierProductDto chainSupplierProductDto, ChainSupplierDto chainSupplierDto) throws Exception{
        return null;
    };


    /**
     * <p>获取评论信息</p>
     *
     *
     * @param chainSupplierProductDto  订单调用链数据
     * @return RoomDto 对象数据
     */
    @Override
    public ResultVo queryComments(ChainSupplierProductDto chainSupplierProductDto, ChainSupplierDto chainSupplierDto) throws Exception{
        return null;
    };


    /**
     * <p>查询物流信息</p>
     *
     *
     * @param chainSupplierProductDto  订单调用链数据
     * @return RoomDto 对象数据
     */
    @Override
    public ResultVo queryLogisticsInfo(ChainSupplierProductDto chainSupplierProductDto, ChainSupplierDto chainSupplierDto) throws Exception{
        return null;
    };

    /**
     * <p>将供应链商品信息写入店铺信息</p>
     *
     *
     * @param chainSupplierProductDto
     * @return RoomDto 对象数据
     */
    @Override
    public ResultVo insertSingleProduct(ChainSupplierProductDto chainSupplierProductDto, ChainSupplierDto chainSupplierDto) throws Exception{
        return null;
    }

    @Override
    public ResultVo callBackChainProduct(String param,ChainSupplierDto tmpChainSupplierDto) {
        return new ResultVo(ResultVo.CODE_OK,ResultVo.MSG_OK);
    }

    ;

}
